package top.mhtools.pigeon.ding.service.support;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import top.mhtools.pigeon.common.enums.DingMessageTypeEnum;
import top.mhtools.pigeon.common.persistence.DingRequest;
import top.mhtools.pigeon.ding.dto.DingMessageReqDTO;

/**
 * Link类型消息发送器
 *
 * @author machen
 * @date 2020/6/16 20:40
 */
@Slf4j
@Component
public class DingMessageLinkSolver extends AbstractDingMessageExecuteStrategy {

    @Override
    public void execute(DingRequest dingRequest) {
        String serverUrl = dingtalkUrl + "?access_token=" + dingRequest.getToken();
        DingMessageReqDTO.LinkDTO linkDTO = dingRequest.getLink();

        // 指定消息类型
        DingTalkClient client = new DefaultDingTalkClient(serverUrl);
        OapiRobotSendRequest request = new OapiRobotSendRequest();
        request.setMsgtype(DingMessageTypeEnum.LINK.getName());

        OapiRobotSendRequest.Link link = new OapiRobotSendRequest.Link();
        link.setPicUrl(linkDTO.getPicUrl());
        link.setMessageUrl(linkDTO.getMessageUrl());
        link.setText(linkDTO.getText());
        link.setTitle(linkDTO.getTitle());

        request.setLink(link);

        sendMessage(client, request);
    }

    @Override
    public String supports() {
        return DingMessageTypeEnum.LINK.getName();
    }
}
